<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html>

<head>
    <meta charset="UTF-8">
    <title>终端列表</title>
    <meta name="ctx" th:content="${#httpServletRequest.getContextPath()}"/>
    <link rel="stylesheet" th:href="@{/lib/flatpickr/flatpickr.min.css}">
    <link rel="stylesheet" th:href="@{/css/admin/base-v2.css}">
    <link rel="stylesheet" th:href="@{/lib/font-awesome/4.7.0/css/font-awesome.min.css}">
    <link rel="stylesheet" th:href="@{/css/admin/app.css}">
</head>

<body class="page-device">
<div class="app-page">
    <div class="app-page-header">
        <div class="page-title">终端管理>终端列表</div>
    </div>
    <div class="app-page-content">
        <div class="app-card">
            <div class="app-card-body">

                <div class="z-df">
                    <!-- 过滤器 -->
                    <form class="filter z-flex">
                        <!-- 选项切换 -->
                        <div class="j-option-switch filter-item">
                            <div class="option form-item">
                                <select id="searchType" name="searchType" data-content-selector=".search">
                                    <option value="default">-请选择-</option>
                                    <option value="deviceNum">机器编码</option>
                                    <option value="deviceIp">IP地址</option>
                                    <option value="deviceMac">mac地址</option>
                                    <option value="deviceStatus">在线状态</option>
                                </select>
                            </div>
                            <div class="content z-mal">

                            </div>
                        </div>

                        <a class="z-btn z-primary" onclick="queryDeviceInfo()">查询</a>
                    </form>

                    <div class="z-btn-group">
                        <button data-state="5" class="j-btn-control z-btn z-default">移动分组</button>
                        <button data-state="4" class="j-btn-control z-btn z-default">远程关机</button>
                        <button data-state="3" class="j-btn-control z-btn z-default">重启</button>
                        <button data-state="2" class="j-btn-control z-btn z-default" disabled>终端升级</button>
                        <button data-state="1" class="j-btn-control z-btn z-default">设置开关机</button>
                        <button data-state="0" class="j-btn-control z-btn z-default">音量</button>
                    </div>
                </div>

                <!-- 关联的选项 -->
                <div class="search z-hide">
                    <div>
                        <div class="form-item">
                            <input type="text" placeholder="请输入查询关键字" id="default" name="default">
                        </div>
                    </div>
                    <div>
                        <div class="form-item">
                            <input type="text" placeholder="请输入查询机器编码" id="deviceNum" name="deviceNum">
                        </div>
                    </div>
                    <div>
                        <div class="form-item">
                            <input type="text" placeholder="请输入查询IP地址" id="deviceIp" name="deviceIp">
                        </div>
                    </div>
                    <div>
                        <div class="form-item">
                            <input type="text" placeholder="请输入查询MAC地址" id="deviceMac" name="deviceMac">
                        </div>
                    </div>
                    <div>
                        <div class="form-item">
                            <select id="deviceStatus" name="onlineStatus">
                                <option value="">所有</option>
                                <option value="0">在线</option>
                                <option value="1">离线</option>
                            </select>
                        </div>
                    </div>
                </div>
                <!-- 关联的选项 End -->
                <div class="app-list-total z-mab">
                    <span>总计：<i th:text="${total}"></i> 在线：<i><a th:text="${deviceOnlineCount}" href="javascript:void(0);" onclick="queryDeviceInfobyDeviceStatus('0')"></a></i> 离线：<i><a th:text="${deviceOfflineCount}" href="javascript:void(0);" onclick="queryDeviceInfobyDeviceStatus('1')"></a></i></span>
                </div>

                <div class="container">
                    <!-- 设备分组 -->
                    <div class="device-group">
                        <div class="device-title">终端分组</div>
                        <div class="body">
                            <div class="head">
                                <div class="btn-group z-flex">
                                    <div class="btn btn-add">
                                        <i class="fa fa-plus"></i>
                                    </div>
                                    <div class="btn btn-edit">
                                        <i class="fa fa-pencil"></i>
                                    </div>
                                    <div class="btn btn-del">
                                        <i class="fa fa-trash"></i>
                                    </div>
                                </div>
                                <div class="handle z-dropdown">
                                    <span class="dropdown-title">操作选项</span>
                                    <ul>
                                        <li>
                                            <a onclick="openShutdown()">远程关机</a>
                                            <a onclick="openRestart()">重启</a>
                                            <a onclick="openDevicePower()">设置开关机</a>
                                            <a onclick="openDeviceSound()">音量</a>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                            <div class="tree-js"></div>
                        </div>
                    </div>

                    <!-- 列表数据 -->
                    <table class="z-table">
                        <thead>
                            <th>
                                <input type="checkbox" class="j-checkbox-all input">
                            </th>
                            <th>#</th>
                            <th>机器码</th>
                            <th>MAC</th>
                            <th>机型</th>
                            <th>IP</th>
                            <th class="z-center">在线状态</th>
                            <th>mips版本</th>
                            <th>固件版本</th>
                            <th>操作</th>
                        </thead>
                        <tbody th:each="resultList : ${resultList}">
                            <tr th:attr="
                                        data-id=${resultList.deviceInfoId},
                                        data-device-num=${resultList.deviceNum},
                                        data-device-mac=${resultList.deviceMac},
                                        data-device-model=${resultList.deviceModel}">
                                <td>
                                    <input type="checkbox" class="j-checkbox input">
                                </td>
                                <td><span th:text="${resultList.deviceInfoId}"/></td>
                                <td>
                                    <a class="j-open-update"><span th:text="${resultList.deviceNum}"/></a>
                                </td>
                                <td><span th:text="${resultList.deviceMac}"/></td>
                                <td><span th:text="${resultList.deviceModel}"/></td>
                                <td><span th:text="${resultList.deviceIp}"/></td>
                                <td class="z-center" style="color: #gray;">
                                    <span th:if="${resultList.deviceStatus} eq '0'"><i class="fa fa-check" style="color: #18da18;"></i></span>
                                    <span th:if="${resultList.deviceStatus} eq '1'"><i class="fa fa-close" ></i></span>
                                </td>
                                <td><span th:text="${resultList.deviceSoftVer}"/></td>
                                <td><span th:text="${resultList.deviceSysVer}"/></td>
                                <td>
                                    <a onclick="deviceLog(this)" th:attr="data-device-mac=${resultList.deviceMac}">日志</a>
                                </td>
                            </tr>
                        </tbody>
                    </table>

                </div>

                <!-- 分页信息 -->
                <div class="app-card-body">
                    <!-- 前端分页 -->
                    <div class="pagination z-pat z-fr"></div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 设备详情页 -->
<div class="popup-update popup-default z-popup z-fixed z-df z-flex-cc z-hide">
    <div class="bg" data-popup-close></div>
    <div class="body">
        <div class="title">设备详情</div>
        <div class="content" id="updateDiv">
        </div>
    </div>
</div>

<!-- 分组 -->
<div class="popup-default z-popup z-fixed z-df z-flex-cc z-hide">
    <div class="bg" data-popup-close></div>
    <!-- 新增分组 -->
    <div class="popup-group-add section z-hide">
        <div class="title">新增终端分组</div>
        <div class="content">
            <form>
                <div class="form-item row">
                    <div class="label" style=" width: 102px; ">
                        <label>父级分组名</label>
                    </div>
                    <input type="hidden" id="parentId" name="parentId" disabled>
                    <input type="text" id="parentName" name="parentName" disabled>
                </div>
                <div class="form-item row">
                    <div class="label">
                        <label>分组名称</label>
                    </div>
                    <input type="text" id="groupName" name="groupName">
                </div>
                <div class="form-item row">
                    <div class="label">
                        <label>分组编码</label>
                    </div>
                    <input type="text" id="groupCode" name="groupCode">
                </div>
            </form>
            <div class="z-btn-group">
                <a class="z-btn z-primary" onclick="saveDeviceGroup()">确定</a>
                <a class="z-btn z-default" data-popup-close>取消</a>
            </div>
        </div>
    </div>
    <!-- 分组编辑 -->
    <div class="popup-group-edit section z-hide">
        <div class="title">编辑分组信息</div>
        <div class="content">
            <form class="z-form z-align">
                <div class="form-item row">
                    <div class="label">
                        <label>分组名称</label>
                    </div>
                    <input type="hidden" id="deviceGroupIdEdit" name="deviceGroupIdEdit">
                    <input type="hidden" id="parentIdEdit" name="parentIdEdit">
                    <input type="text" id="groupNameEdit" name="groupNameEdit">
                </div>
                <div class="form-item row">
                    <div class="label">
                        <label>分组编码</label>
                    </div>
                    <input type="text" id="groupCodeEdit" name="groupCodeEdit">
                </div>
            </form>
            <div class="z-btn-group">
                <a class="z-btn z-primary" onclick="updateDeviceGroup()">确定</a>
                <a class="z-btn z-default" data-popup-close>取消</a>
            </div>
        </div>
    </div>
</div>


<!-- 设备操作 -->
<div class="popup-default z-popup z-fixed z-df z-flex-cc z-hide">
    <div class="bg" data-popup-close></div>
    <!-- 音量设置 -->
    <div class="popup-device-sound section z-hide">
        <div class="title">音量设置</div>
        <div class="content">
            <form>
                <input type="hidden" name="deviceMacSound" id="deviceMacSound">
                <input type="hidden" name="groupCodeSound" id="groupCodeSound">
                <div class="form-item row">
                    <div class="label">
                        <label>音量值为0~100</label>
                    </div>
                    <input type="text" name="deviceVolume" id="deviceVolume" value="20">
                </div>
            </form>
            <div class="z-btn-group">
                <a class="z-btn z-primary" onclick="setVolume()">确定</a>
                <a class="z-btn z-default" data-popup-close>取消</a>
            </div>
        </div>
    </div>
    <!-- 电源 -->
    <div class="popup-device-power section z-hide">
        <div class="title">开关机设置</div>
        <div class="content">
            <form>
                <input type="hidden" name="deviceMacPower" id="deviceMacPower">
                <input type="hidden" name="groupCodePower" id="groupCodePower">
                <div class="form-item row">
                    <div class="label">
                        <label>启用与否</label>
                    </div>
                    <div class="body">
                        <input type="checkbox" id="useTimer" name="useTimer" class="input-switch">
                    </div>
                </div>
                <div class="form-item row">
                    <div class="label">
                        <label>开机时间</label>
                    </div>
                    <div class="body">
                        <input type="datetime" name="bootTimePower" id="bootTimePower" class="flatpickr-hour" value="00:00">
                    </div>
                </div>
                <div class="form-item row">
                    <div class="label">
                        <label>关机时间</label>
                    </div>
                    <div class="body">
                        <input type="datetime" name="offTimePower" id="offTimePower" class="flatpickr-hour" value="00:00">
                    </div>
                </div>
            </form>
            <div class="z-btn-group">
                <a class="z-btn z-primary" onclick="setTimerShutDownOrStart()">确定</a>
                <a class="z-btn z-default" data-popup-close>取消</a>
            </div>
        </div>
    </div>

    <!-- 移动分组 -->
    <div class="popup-move-groupCode section z-hide">
        <div class="title">移动到分组</div>
        <div class="content">
            <input type="hidden" name="deviceInfoIdMove" id="deviceInfoIdMove">
            <form class="tree-js-move">
            </form>
            <div class="z-btn-group" style="margin-top:30px">
                <a class="z-btn z-primary" onclick="moveDeviceGroup()">确定</a>
                <a class="z-btn z-default" data-popup-close>取消</a>
            </div>
        </div>
    </div>
</div>

<script th:src="@{/lib/jquery.min.js}"></script>
<script th:src="@{/lib/layer-v3.1.1/layer/layer.js}"></script>
<script th:src="@{/js/form.js}"></script>
<script th:src="@{/js/popup.js}"></script>
<script th:src="@{/js/admin/app.js}"></script>
<script th:src="@{/js/tree.js}"></script>
<script th:src="@{/js/pagination.js}"></script>
<script th:src="@{/lib/flatpickr/flatpickr.js}"></script>
<script th:src="@{/lib/flatpickr/zh.js}"></script>
<script type="text/javascript" th:inline="javascript">
    //获取context path
    //var context = $("meta[name='ctx']").attr("content");
    var context = $("meta[name='ctx']").attr("content") || '';


    // 查询条件回显
    // 分组编号
    var groupCodeReview = [[${groupCodeReview}]];
    // 终端机器码
    var deviceNumReview = [[${deviceNumReview}]];
    // 终端Ip
    var deviceIpReview = [[${deviceIpReview}]];
    // 终端mac
    var deviceMacReview = [[${deviceMacReview}]];
    // 终端状态
    var deviceStatusReview = [[${deviceStatusReview}]];
    if(deviceNumReview != null && deviceNumReview != "") {
        document.getElementById('searchType').value="deviceNum";
    }
    if(deviceIpReview != null && deviceIpReview != "") {
        document.getElementById('searchType').value="deviceIp";
    }
    if(deviceMacReview != null && deviceMacReview != "") {
        document.getElementById('searchType').value="deviceMac";
    }
    if(deviceStatusReview != null) {
        document.getElementById('searchType').value="deviceStatus";
    }

    //分页
    var pageNum =  [[${pageNum}]];
    var pageSize = [[${pageSize}]];
    var total = [[${total}]];
    var searchValue;
    var searchType;
    var url = "";
    // 查询条件
    if(deviceNumReview != null && deviceNumReview != "") {
        searchValue = deviceNumReview;
        searchType = "deviceNum";
    }
    if(deviceIpReview != null && deviceIpReview != "") {
        searchValue = deviceIpReview;
        searchType = "deviceIp";
    }
    if(deviceMacReview != null && deviceMacReview != "") {
        searchValue = deviceMacReview;
        searchType = "deviceMac";
    }
    if(deviceStatusReview != null) {
        searchValue = deviceStatusReview;
        searchType = "deviceStatus";
    }
    var pagination = new Pagination(".pagination", {
        pageIndex: pageNum, // 当前页数
        pageSize: pageSize, // 每页数量
        count: total, // 总条数
        maxButtonCount: 5, // 分页按钮数量(可选)
        // 分页切换事件
        onPageChanged: function(pageIndex) {
            if(searchValue == null || searchType == null) {
                window.location.href = context + "/deviceInfo/listByPage?pageNum=" + pageIndex + "&pageSize=" + pageSize ;
            } else {
                window.location.href = context + "/deviceInfo/listByPage?pageNum=" + pageIndex + "&pageSize=" + pageSize + "&" + searchType + "=" + searchValue;
            }

        }
    })

    // 切换到指定分页
    // pagination.options.pageIndex = 5;
    // pagination.render();


    // 当前所选终端分组
    var deviceGroup;
    // 移动分组里的所选终端分组
    var deviceGroup1;
    // 树形组件
    var tree;
    // 移动分组里的树形组件
    var tree1;

    //加载分组数据
    var str = "";
    $.ajax({
        type: 'post',
        dataType: 'json',
        url: context +'/deviceGroup/listDeviceGroupTree',
        data: {},
        cache: false,
        async: true,
        success: function (result) {
            // 终端页面上的树形控件
            tree = new Tree('.tree-js', {
                type: 1,
                data: result.data,
                // 当前选中的数据
                onChange: function(trreItem) {
                    // console.log('onChange');
                    // console.log(trreItem);
                    if (trreItem.checked) {
                        deviceGroup = trreItem;
                    } else {
                        deviceGroup = null;
                    }
                },
                // 当前选中标签
                onClick: function(trreItem) {
                    console.log('trreItem');
                    console.log(trreItem);
                    var URL = context + "/deviceInfo/listByPage?groupCode=" + trreItem.groupCode+"&groupIndex="+trreItem.id;
                    window.location.href = (URL);
                }
            })

            if(App.query.groupIndex){
                $('.device-group .tree-item[data-id='+App.query.groupIndex+']').addClass('active')
            }
            

            // 移动分组页面上的树形控件
            tree1 = new Tree('.tree-js-move', {
                type: 1,
                data: result.data,
                // 当前选中的数据
                onChange: function(trreItem) {
                    console.log('move-onChange');
                    console.log(trreItem);
                    if (trreItem.checked) {
                        deviceGroup1 = trreItem;
                    } else {
                        deviceGroup1 = null;
                    }
                }

            })

        }
    });


    // 获取树形控件数据
    function getTreeData() {

        const treeData = tree.getData();

        function trreePipe(parent, pipe) {
            pipe(parent);

            if (parent.children) {
                for (let i = 0; i < parent.children.length; i++) {
                    const treeItem = parent.children[i];
                    if (pipe(treeItem) === false) {
                        return false;
                    }

                    if (treeItem.children) {
                        trreePipe(treeItem, pipe)
                    }
                }
            }
        }

        for (let i = 0; i < treeData.length; i++) {
            trreePipe(treeData[i], function(treeItem) {
                if (treeItem.checked) {
                    console.log('treeItem');
                    console.log(treeItem);
                }
            })
        }
    }

    // 过滤器 - 从URL加载搜索数据
    $(".filter").formLoad(App.query);

    // 过滤器 - 加载select联动内容
    $('[name="searchType"] option').each(function(index, element) {
        if (element.selected == true) {
            $('.j-option-switch .content').html($(".search").children('div').eq(index).html());
            // 查询条件回显
            $('#deviceNum').val(deviceNumReview);
            $('#deviceIp').val(deviceIpReview);
            $('#deviceMac').val(deviceMacReview);
            $('#deviceStatus').val(deviceStatusReview);
        }
    })

    // 设备 - 弹出详情页
    $(document).on('click', '.j-open-update', function() {
        var data = $(this).parents('tr')[0].dataset;
        var deviceInfoId = data.id; // 终端id
        $('#updateDiv').empty();
        $.ajax({
            type: 'post',
            dataType: 'json',
            url: context + '/deviceInfo/getDeviceInfoDetailAjax',
            data: {deviceInfoId:deviceInfoId},
            cache: false,
            async: true,
            success: function (result) {
                if(result.code == 0) { // 查询成功
                    var detail = result.data;
                    var statusHtml = "";
                    if(detail.deviceStatus == 0) {
                        statusHtml = "<i class='fa fa-check' style='color: #18da18;'></i>";
                    }else {
                        statusHtml = "<i class='fa fa-close' ></i>";
                    }
                    var html =
                            "<form>\n" +
                            "<div class='form-item row'>\n" +
                            "   <div class='label'>\n" +
                            "       <label>机器码：</label>\n" +
                            "   </div>\n" +
                            "   <div style='width: 200px'>\n" +
                            "       <input type='hidden' name='deviceInfoId_update' id='deviceInfoId_update'>\n" +
                            "       <input type='text' name='deviceNum_update' id='deviceNum_update'>\n" +
                            "   </div>\n" +
                            "</div>" +
                            "<div class='form-item row'>\n" +
                            "       <div class='label'>\n" +
                            "           <label>MAC：</label>\n" +
                            "       </div>\n" +
                            "       <div style='width: 200px'>\n" +
                            "           <span>"+detail.deviceMac+"</span>\n" +
                            "       </div>\n" +
                            "</div>" +
                            "<div class='form-item row'>\n" +
                            "       <div class='label'>\n" +
                            "           <label>IP：</label>\n" +
                            "       </div>\n" +
                            "       <div style='width: 200px'>\n" +
                            "           <span>"+detail.deviceIp+"</span>\n" +
                            "       </div>\n" +
                            "</div>" +
                            "<div class='form-item row'>\n" +
                            "       <div class='label'>\n" +
                            "           <label>在线状态：</label>\n" +
                            "       </div>\n" +
                            "       <div style='width: 200px'>\n" +
                            "           <span>"+detail.deviceStatus+"</span>\n" +
                            "       </div>\n" +
                            "</div>" +
                            "<div class='form-item row'>\n" +
                            "       <div class='label'>\n" +
                            "           <label>机型：</label>\n" +
                            "       </div>\n" +
                            "       <div style='width: 200px'>\n" +
                            "           <span>"+detail.deviceModel+"</span>\n" +
                            "       </div>\n" +
                            "</div>" +
                            "<div class='form-item row'>\n" +
                            "       <div class='label'>\n" +
                            "           <label>mips版本：</label>\n" +
                            "       </div>\n" +
                            "       <div style='width: 200px'>\n" +
                            "           <span>"+statusHtml+"</span>\n" +
                            "       </div>\n" +
                            "</div>" +
                            "<div class='form-item row'>\n" +
                            "       <div class='label'>\n" +
                            "           <label>固件版本：</label>\n" +
                            "       </div>\n" +
                            "       <div style='width: 200px'>\n" +
                            "           <span>"+detail.deviceSysVer+"</span>\n" +
                            "       </div>\n" +
                            "</div>" +
                            "<div class='form-item row'>\n" +
                            "       <div class='label'>\n" +
                            "           <label>当前播放：</label>\n" +
                            "       </div>\n" +
                            "       <div style='width: 200px'>\n" +
                            "           <span>"+detail.playJson+"</span>\n" +
                            "       </div>\n" +
                            "</div>" +
                            "<div class='form-item row'>\n" +
                            "       <div class='label'>\n" +
                            "           <label>开机时间：</label>\n" +
                            "       </div>\n" +
                            "       <div style='width: 200px'>\n" +
                            "           <span>"+detail.bootTime+"</span>\n" +
                            "       </div>\n" +
                            "</div>" +
                            "<div class='form-item row'>\n" +
                            "       <div class='label'>\n" +
                            "           <label>关机时间：</label>\n" +
                            "       </div>\n" +
                            "       <div style='width: 200px'>\n" +
                            "           <span>"+detail.offTime+"</span>\n" +
                            "       </div>\n" +
                            "</div>" +
                            "<div class='form-item row'>\n" +
                            "       <div class='label'>\n" +
                            "           <label>音量：</label>\n" +
                            "       </div>\n" +
                            "       <div style='width: 200px'>\n" +
                            "           <span>"+detail.deviceVolume+"</span>\n" +
                            "       </div>\n" +
                            "</div>" +
                            "<div class='form-item row'>\n" +
                            "       <div class='label'>\n" +
                            "           <label>地址：</label>\n" +
                            "       </div>\n" +
                            "       <div style='width: 200px'>\n" +
                            "           <span>"+detail.deviceProvince+detail.deviceCity+detail.deviceCounty+"</span>\n" +
                            "       </div>\n" +
                            "</div>\n" +
                            "</form>" +
                            "<div class='z-btn-group'>\n" +
                            "<button class='z-btn z-primary' id='updateDeviceNumBtn' name='updateDeviceNumBtn' onclick='updateDeviceNum()'>保存</button> \n" +
                            "<button class='z-btn z-default' data-popup-close>取消</button> \n" +
                            "</div>";

                    $('#updateDiv').append(html);
                    // 打开
                    popup.open(".popup-update");
                    // 表单复制
                    $(".popup-update").formLoad({
                        deviceInfoId_update: data.id,
                        deviceNum_update:data.deviceNum
                    });
                } else { // 查询失败，刷新当前页面
                    alert(result.msg);
                    window.location.href = context + "/deviceInfo/listByPage";;
                }
            }
        });
    })

    // 机器码输入框 - 点击
/*    $(document).on('change', '.field',function () {
        $("#updateDeviceNumBtn").removeAttr("disabled");
        $("#updateDeviceNumBtn").attr("disabled",false);
    })*/

    // 设备 - 按钮
    $(document).on('click', '.j-btn-control', function() {
        var state = $(this).data('state');
        var count = 0;
        var deviceMac = ""; // 终端mac地址
        var deviceModel = ""; // 机型。用于获取apk列表
        var deviceNum = ""; // 机器码
        $('tbody [type="checkbox"]').each(function(index, element) {
            if (element.checked == true) {
                var data = $(element).parents('tr')[0].dataset;
                deviceModel = data.deviceModel;
                if(count == 0) {
                    deviceMac = data.deviceMac;
                    deviceNum = data.deviceNum;
                }else{
                    deviceMac = deviceMac +","+ data.deviceMac;
                    deviceNum = deviceNum + "," + data.deviceNum;
                }
                count++;
            }
        })

        if (count == 0) {
            popup.tipsTop("请先选择数据", 'warning');
            return false;
        }

        switch (Number(state)) {
            case 4: //远程关机
                shutdown(deviceMac,"","是否强制关闭设备" + deviceNum);
                break;
            case 3: //重启
                restart(deviceMac,"",deviceNum);
                break;
            case 2: //终端升级
                var ids = getSelecteId();
                App.layerOpen({
                    url: context + '/deviceUpgrade/setDeviceUpdateList?deviceInfoId=' + getSelecteId() + '&deviceModel='  + deviceModel ,
                    title: "终端升级",
                });
                break;
            case 1: //设置定时开关机
                popup.open(".popup-device-power");
                // 表单复制
                $(".popup-device-power").formLoad({
                    deviceMacPower: deviceMac
                });
                break;
            case 0: //音量
                popup.open(".popup-device-sound")
                // 表单复制
                $(".popup-device-sound").formLoad({
                    deviceMacSound: deviceMac
                });
                break;
            case 5: // 移动分组
                popup.open(".popup-move-groupCode")
                // 表单复制
                $(".popup-move-groupCode").formLoad({
                    deviceInfoIdMove: getSelecteId()
                });
                break
        }

    })

    // 分组 - 新增
    $(document).on('click', '.device-group .btn-add', function() {
        if (groupCheckboxVerify()) {
            $('.popup-group-add').formLoad({
                parentId: deviceGroup.deviceGroupId,
                parentName: deviceGroup.label
            });
            popup.open(".popup-group-add")
        }
    })

    // 分组 - 编辑
    $(document).on('click', '.device-group .btn-edit', function() {
        if (groupCheckboxVerify()) {
            if(deviceGroup.deviceGroupId == null || deviceGroup.deviceGroupId == "") {
                popup.tipsTop("不能编辑全部分组", 'warning');
                return false;
            }else {
                $('.popup-group-edit').formLoad({
                    deviceGroupIdEdit:deviceGroup.deviceGroupId,
                    groupNameEdit:deviceGroup.label,
                    groupCodeEdit:deviceGroup.groupCode,
                    parentIdEdit:deviceGroup.pid
                })
                // 弹出分组编辑
                popup.open(".popup-group-edit")
            }
        }
    })

    // 分组 - 删除
    $(document).on('click', '.device-group .btn-del', function() {
        if (groupCheckboxVerify()) {
            if(deviceGroup.deviceGroupId == null || deviceGroup.deviceGroupId == "") {
                popup.tipsTop("不能删除全部分组", 'warning');
                return false;
            }else {
                if (confirm('确认是否删除')) {
                    deleteDeviceGroup(deviceGroup);
                }
            }
        }
    })

    // 分组 - 批量操作
    /*$(document).on('click', '.device-group .handle a', function() {
        if (groupCheckboxVerify()) {
            popup.tipsTop("操作成功", 'success');
        }
    })*/

    // 分组选择验证
    function groupCheckboxVerify(params) {
        if (tree.checked.length < 1) {
            popup.tipsTop("请先选择分组", 'warning');
            return false;
        }
        return true;
    }

    // 分组选择验证1
    function groupCheckboxVerify1(params) {
        if (params.checked.length != 1) {
            popup.tipsTop("请选择一个分组", 'warning');
            return false;
        }
        return true;
    }



    // 列表数据选择监听
    $(document).on('click', 'table [type="checkbox"]', function() {
        // 获取选中的数据
        var data = [];
        var count = 0 ;
        $('tbody [type="checkbox"]').each(function(index, element) {
            if (element.checked == true) {
                data.push($(this).parents('tr').data());
                count ++ ;
            }
        })

        //  只能选择一个终端进行升级
        //  条件满足允许操作
        if (count == 1) {
            $('.j-btn-control[data-state=2]')[0].disabled = false;
        } else {
            $('.j-btn-control[data-state=2]')[0].disabled = true;
        }

    })

    // 获取表格中选择的ID
    function getSelecteId() {
        let temp = [];
        $('tbody [type="checkbox"]').each(function(index, element) {
            if (element.checked == true) {
                temp.push($(this).parents('tr,.tr').data('id'));
            }
        })

        return temp.join(',');
    }

    // 设置为中文
    flatpickr.localize(flatpickr.l10ns.zh);
    flatpickr(".flatpickr-hour", {
        enableTime: true,
        noCalendar: true,
        dateFormat: "H:i",
        time_24hr: true
    });

    /**
     * 根据条件查询终端列表
     */
    function queryDeviceInfo() {
        var searchType = $('#searchType option:selected') .val();//查询条件
        var searchValue = $('#'+searchType).val();
        var URL = context + "/deviceInfo/listByPage";
        if("default" != searchType && searchValue != null && searchValue != ""){
            URL = URL + "?"+searchType+"="+searchValue;
        }
        window.location.href = (URL);
    }

    /**
     * 通过在线状态查询终端列表
     */
    function queryDeviceInfobyDeviceStatus(deviceStatus) {
        var url = context + "/deviceInfo/listByPage?deviceStatus=" + deviceStatus;
        window.location.href = (url);
    }

    /**
     * 更新机器码
     */
    function updateDeviceNum() {
        var deviceInfoId = $('#deviceInfoId_update').val();
        var deviceNum = $('#deviceNum_update').val();
        $.ajax({
            type: 'post',
            dataType: 'json',
            url: context + '/deviceInfo/updateDeviceNum',
            data: {deviceInfoId:deviceInfoId,deviceNum:deviceNum},
            cache: false,
            async: true,
            success: function (data) {
                alert(data.msg);
                if(data.code == 0) { //操作成功，刷新当前页面
                    window.location.href = context + "/deviceInfo/listByPage";;
                }
            }
        });
    }

    /**
     * 远程关机
     * @param deviceMac 所选终端的MAC地址
     */
    function shutdown(deviceMac,groupCode,msg) {
        if(confirm(msg)){
            $.ajax({
                type: 'post',
                dataType: 'json',
                url: context + '/deviceInfo/shutdown',
                data: {deviceMacs:deviceMac,groupCode:groupCode},
                cache: false,
                async: true,
                success: function (data) {
                    alert(data.msg);
                    //刷新当前页面
                    window.location.href = context + "/deviceInfo/listByPage";;
                }
            });
        }
    }

    /**
     * 重启
     * @param deviceMac 所选终端的MAC地址
     */
    function restart(deviceMac,groupCode,deviceNum) {
        var msg = "是否重启设备";
        if(confirm(msg + deviceNum)){
            $.ajax({
                type: 'post',
                dataType: 'json',
                url: context + '/deviceInfo/restart',
                data: {deviceMacs:deviceMac,groupCode:groupCode},
                cache: false,
                async: true,
                success: function (data) {
                    alert(data.msg);
                    //刷新当前页面
                    window.location.href = context + "/deviceInfo/listByPage";;
                }
            });
        }
    }

    /**
     * 定时开关机设置
     */
    function setTimerShutDownOrStart() {
        var deviceMacPower = $('#deviceMacPower').val();//终端MAC地址
        var bootTimePower = $('#bootTimePower').val(); //开机时间
        var offTimePower = $('#offTimePower').val();//关机时间
        var useTimer = document.getElementById("useTimer").checked;//是否启用，暂时默认1  1:启用 0:关闭
        if(useTimer) {
            useTimer = 1;
        } else {
            useTimer = 0;
        }
        if(groupCheckboxVerify()) {
            var groupCode = "";//分组编码
            for(var i=0;i<tree.checked.length;i++){
                var item = tree.checked[i];
                if(item.deviceGroupId != null && item.deviceGroupId != "") {
                    groupCode = groupCode + "," + item.groupCode;
                }
            }
        }
        console.log(groupCode);
        $.ajax({
            type: 'post',
            dataType: 'json',
            url: context + '/deviceInfo/setTimerShutDownOrStart',
            data: {deviceMacs:deviceMacPower,useTimer:useTimer,bootTime:bootTimePower,offTime:offTimePower,groupCode:groupCode},
            cache: false,
            async: true,
            success: function (data) {
                alert(data.msg);
                if(data.code == 0) { //操作成功，刷新当前页面
                    window.location.href = context + "/deviceInfo/listByPage";;
                }
            }
        });
    }

    /**
     * 设置音量
     */
    function setVolume() {
        var deviceMacs = $('#deviceMacSound').val(); //终端mac地址
        var deviceVolume = $('#deviceVolume').val(); //音量
        if(groupCheckboxVerify()) {
            var groupCode = "";//分组编码
            for(var i=0;i<tree.checked.length;i++){
                var item = tree.checked[i];
                if(item.deviceGroupId != null && item.deviceGroupId != "") {
                    groupCode = groupCode + "," + item.groupCode;
                }
            }
        }
        console.log(groupCode);
        $.ajax({
            type: 'post',
            dataType: 'json',
            url: context + '/deviceInfo/setVolume',
            data: {deviceMacs:deviceMacs,deviceVolume:deviceVolume,groupCode:groupCode},
            cache: false,
            async: true,
            success: function (data) {
                alert(data.msg);
                if(data.code == 0 ) { //操作成功，刷新当前页面
                    window.location.href = context + "/deviceInfo/listByPage";
                }
            }
        });
    }

    /**
     * 获取终端日志列表
     * @param btn
     */
    function deviceLog(btn) {
        var data = btn.dataset;
        var deviceMac = data.deviceMac;
        App.layerOpen({
            url: context + '/deviceInfo/deviceLogList?deviceMac=' + deviceMac,
            title: "终端日志",
        });
    }

    /**
     * 新增终端分组
     */
    function saveDeviceGroup() {
        var parentId = $('#parentId').val();    //父分组
        var groupName = $('#groupName').val();  //分组名称
        var groupCode = $('#groupCode').val();  //分组编码
        $.ajax({
            type:"POST",
            url: context + "/deviceGroup/saveDeviceGroup",
            data: {deviceGroupId:"",parentId:parentId,groupName:groupName,groupCode:groupCode,updateTime:""},
            dataType:"json",
            success:function(result){
                alert(result.msg);
                if(result.code == 0) {
                    window.location.href = context + "/deviceInfo/listByPage";;
                }
            }
        });
    }

    /**
     * 删除终端分组
     */
    function deleteDeviceGroup(deviceGroup) {
        var deviceGroupId = deviceGroup.deviceGroupId;
        $.ajax({
            type:"POST",
            url: context + "/deviceGroup/deleteDeviceGroup",
            data: {deviceGroupId:deviceGroupId},
            dataType:"json",
            success:function(result){
                alert(result.msg);
                if(result.code == 0) {
                    window.location.href = context + "/deviceInfo/listByPage";;
                }
            }
        });
    }

    /**
     * 更新终端分组
     */
    function updateDeviceGroup() {
        var deviceGroupId = $('#deviceGroupIdEdit').val();
        var parentId = $('#parentIdEdit').val();
        var groupName = $('#groupNameEdit').val();
        var groupCode = $('#groupCodeEdit').val();
        $.ajax({
            type:"POST",
            url: context + "/deviceGroup/updateDeviceGroup",
            data: {deviceGroupId:deviceGroupId,parentId:parentId,groupName:groupName,groupCode:groupCode,updateTime:""},
            dataType:"json",
            success:function(result){
                alert(result.msg);
                if(result.code == 0) {
                    window.location.href = context + "/deviceInfo/listByPage";;
                }
            }
        });
    }

    //终端分组远程关机
    function openShutdown() {
        if(groupCheckboxVerify()) {
            var groupCode = "";
            for(var i=0;i<tree.checked.length;i++){
                var item = tree.checked[i];
                if(item.deviceGroupId != null && item.deviceGroupId != "") {
                    groupCode = groupCode + "," + item.groupCode;
                }
            }
            console.log(groupCode);
            shutdown("",groupCode,"是否强制关闭该分组下的设备");
        }
    }

    //终端分组重启
    function openRestart() {
        if(groupCheckboxVerify()) {
            var groupCode = "";
            for(var i=0;i<tree.checked.length;i++){
                var item = tree.checked[i];
                if(item.deviceGroupId != null && item.deviceGroupId != "") {
                    groupCode = groupCode + "," + item.groupCode;
                }
            }
            console.log(groupCode);
            restart("",groupCode,"");
        }
    }

    /**
     * 终端分组打开定时开关机设置页面
     */
    function openDevicePower() {
        if(groupCheckboxVerify()) {
            popup.open(".popup-device-power");
            // 表单复制
            $(".popup-device-power").formLoad({
                groupCodePower: deviceGroup.groupCode
            });
        }
    }

    /**
     * 终端分组打开音量设置页面
     */
    function openDeviceSound() {
        if(groupCheckboxVerify()) {
            popup.open(".popup-device-sound");
            // 表单复制
            $(".popup-device-sound").formLoad({
                groupCodeSound: deviceGroup.groupCode
            });
        }
    }

    /**
     * 移动终端到分组
     */
    function moveDeviceGroup() {
        if(groupCheckboxVerify()) {
            var groupCode = deviceGroup1.groupCode;  // 分组编码
            var deviceInfoIdMove = $("#deviceInfoIdMove").val();    // 终端id(逗号隔开)
            $.ajax({
                type: "POST",
                url: context + "/deviceGroup/moveDeviceGroup",
                data: {groupCode: groupCode, deviceInfoIds: deviceInfoIdMove},
                dataType: "json",
                success: function (result) {
                    alert(result.msg);
                    if (result.code == 0) {
                        window.location.href = context + "/deviceInfo/listByPage";
                    }
                }
            });
        }
    }
</script>
</body>

</html>